package com.my.jli.configuration;

import com.my.jli.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName WebConfig
 * @Description
 * @Author MaYu
 * @Date 2022/3/28 9:57
 * @Version 1.0
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {

    //文件保存路径
    @Value("${file-save-path}")
    private String fileSavePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
          配置映射资源
          如果访问的资源路径是以"/upload/"开头，则映射到fileSavePath（yml文件中配置）这个目录
         */
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + fileSavePath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/toSign", "/toResource");
    }
}
